package day240925;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;



@WebServlet("/protectedPage")
public class ProtectedPageServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 检查用户是否已经登录（会话中是否有用户属性）
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute("user") != null) {
            // 用户已登录，显示受保护的页面
            response.setContentType("text/html;charset=UTF-8");
            response.sendRedirect(request.getContextPath()+"/dashboard.jsp");
       }else {
            // 用户未登录，重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/login1.html");
        }
    }
}